package com.my.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.ResultPage;
import com.my.bean.TrMeal;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.TrMealService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-09-13
 */
@Controller
@RequestMapping("/trMeal")
public class TrMealController {
    @Autowired
    TrMealService trMealService;
    Logger logger=Logger.getLogger(TrMealController.class);






    //查询所有套餐
    @RequestMapping("/list")
    @ResponseBody
    public List<TrMeal> findAllMeal(HttpSession session) {

        Map map=new HashMap<>();
                Long areaid = ((User)session.getAttribute("user")).getAreaId();
          map.put("areaid",areaid);
        return  trMealService.selectByMap(map);
    }

    @RequestMapping("/selectMeal")
    @ResponseBody
    public ResultPage<TrMeal> findAllMeal(String lin, String data, HttpSession session, VoPage voPage){
        boolean admin=false;
//如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<TrMeal> resultPage = trMealService.selectMyPage(map,voPage,areaid,admin);



        /*List<TrMeal> list = new ArrayList<TrMeal>();
        list = JSONObject.parseArray(resultPage, TrMeal.class);*/

        return resultPage;
    }




    /**
     * 添加套餐
     * @param param
     * @param session
     * @return
     */
    @RequestMapping("/insertMeal")
    @ResponseBody
    public String insertMeal(String param,HttpSession session){
        //把JSON字符串转换成以哦个实体类对象
        TrMeal trMeal = JSONObject.parseObject(param,TrMeal.class);
        //添加的时候加一个创建时间
        trMeal.setCreateTime(new Date().getTime());
        //添加地区
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        trMeal.setAreaid(areaid);
        //添加套餐
        if(trMealService.insert(trMeal)){
            logger.info("添加套餐成功");
            return "添加成功";
        }else{
            logger.info("添加套餐失败");
            return "添加失败";
        }
    }


    /**
     * 修改套餐
     * @param param
     * @return
     */
    @RequestMapping("/updateMeal")
    @ResponseBody
    public String updateMeal(String param){
        TrMeal trMeal = JSONObject.parseObject(param,TrMeal.class);
        if(trMealService.updateById(trMeal)){
            logger.info("修改套餐成功");
            return "修改成功";
        }else{
            logger.info("修改套餐失败");
            return "修改失败";
        }

    }

    /**
     * 删除套餐
     */
    @RequestMapping("/deleteMeal")
    @ResponseBody
    public boolean deleteMeal(Long id){
        return trMealService.deleteById(id);
    }


    /**
     * 返回套餐页面
     * @return
     */
    @RequestMapping("/meal")
    public String meal(){
        return "meal";
    }

    /**
     * 返回套餐添加页面
     * @return
     */
    @RequestMapping("/mealAdd")
    public String addMeal(){
        return "/Alert/meal_add";
    }


    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping("/meal_edit")
    public String mealEdit(){
        return "/Alert/meal_edit";
    }





}
